package com.ysx.oa.role.service.impl;


import com.ysx.oa.role.bean.RoleBean;
import com.ysx.oa.role.bean.UserRoleBean;
import com.ysx.oa.role.dao.RoleDao;
import com.ysx.oa.role.dao.UserRoleDao;
import com.ysx.oa.role.service.RoleService;
import com.ysx.oa.utils.GetDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;


@Service
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleDao dao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public RoleBean findById(Integer id) {
        RoleBean bean = new RoleBean();
        bean.setId(id);
        return dao.selectOne(bean);
    }

    @Override
    public Integer add(RoleBean bean) {
        RoleBean roleBean=new RoleBean();
        roleBean.setName(bean.getName());
        if(dao.select(roleBean).size()>0){
            return 0;
        }
        bean.setCreateTime(GetDateTime.nowTime());
        bean.setUpdateTime(GetDateTime.nowTime());
        return dao.insert(bean);
    }

    @Override
    public Integer update(RoleBean bean) {
        RoleBean roleBean=new RoleBean();
        roleBean.setName(bean.getName());
        if(dao.select(roleBean).size()>0){
            return 0;
        }
        bean.setUpdateTime(GetDateTime.nowTime());
        return dao.updateByPrimaryKeySelective(bean);
    }

    @Override
    public Integer delete(List<Integer> ids) {
        for(Integer id:ids){
            UserRoleBean userRoleBean=new UserRoleBean();
            userRoleBean.setRoleId(id);
            if(userRoleDao.select(userRoleBean).size()>0){
                return 0;
            }
        }
        return dao.deleteRole(ids);
    }

    @Override
    public List<RoleBean> findAllRole() {
        List<RoleBean> allRole = dao.findAllRole();
        return allRole;
    }


}
